﻿/// <reference path="../../js/jquery-1.11.1.min.js" />
/// <reference path="../../js/jquery-1.11.0-vsdoc.js" />

/*
	子畫面的大小
*/
var program_id = "CTMD0061.aspx";
var _DialogWidth = "870px";
var _DialogHeight = "650px";
var dialogStr = "dialogWidth="+_DialogWidth+";dialogHeight="+_DialogHeight+";center=yes;border=thin;help=no;status=no;resizable=no;";
/*
	事件功能: 查詢資料庫內容,將資料與資料呈現頁面(DataGrid)連繫
	傳入值 : null
	傳出值 : null
*/
function btnSEARCH_OnClick()
{
	try
	{
		//判斷起迄值
		Chk_BETWEEN()
		
	    //Tony 待確定是否要改用 $("form:first :input")
		var chkResult = isValid(document.forms(0).elements);
		if (chkResult) 
		{
			//宣告元件
			var XmlDocument = null;
			
			// 新增XML 第一個節點(<form>) ，並指定id 為BASM080(程式名稱),method為Query(查詢function名稱)
			addFormNode("CTMD0060","Query");
	        
			/* 新增第二層之節點(<command>),指定id 為issue_type(將作為DataTableName,issue_type 為實體DB 之Table名稱)      *
			* 並指定action 為SEL (共分四種action,查詢：SEL 新增：ADD 修改：UPD 刪除：DEL 將提供SQLConverter轉SQL作判斷) */
			addCommandNode("CTM_CUSTOMS_TRANS","sel","pagesize","20");
			
			/* 新增第三層之第一個節點(<parameter>),指定id 為class_level,type為string(共有兩種：文字為string，數值為decimal) *
			* 並指定此節點的Text 為 docmnent.all.rdoClass.value(dropDonwlist之值)                                          */                                            
			addParameterNode("CUSTOMS_TYPE","string",$("#drpCUSTOMS_TYPE").val());
			addParameterNode("SHEET_NO1","string",$("#txtSHEET_NO1").val());
			addParameterNode("SHEET_NO2","string",$("#txtSHEET_NO2").val());
			addParameterNode("CTM_APPLY_NO1","string",$("#txtCTM_APPLY_NO1").val());
			addParameterNode("CTM_APPLY_NO2","string",$("#txtCTM_APPLY_NO2").val());
			addParameterNode("IN_OUT_DATE1","string",$("#txtIN_OUT_DATE1").val());
			addParameterNode("IN_OUT_DATE2","string",$("#txtIN_OUT_DATE2").val());
			addParameterNode("CODE1","string",$("#txtCODE_1").val());
			addParameterNode("CODE2","string",$("#txtCODE_2").val());
			addParameterNode("CUSTOMS_TYPE","string",$("#drpCUSTOMS_TYPE").val());
			
			// 新增第三層第二個節點(<parameter>)，指定id為issue_type,指定type 為 string,指定TEXT為document.all.txtType.value(txtType之value)
			//addParameterNode("issue_type","string",document.all.txtType.value);
			
			// 取得XML字串
			xmlstr = getXML();
			
			//執行語法 (xml資料, 排序欄位, 資料起始位置)	
			XmlDocument = ExecuteQuery(xmlstr,"A.SHEET_NO", 0);
			
			// 測試 htmlDataSet
			var htmlDataSet = new dataSetObject();
			htmlDataSet.load(XmlDocument);

			//資料連繫
			DataBind(XmlDocument);
		}
	 }
	 catch(e)
	 {
		ShowError(e.message);
	 }
}

/*
	事件功能: 開起資料查詢頁面(CTMD0061.aspx)
	傳入值 : null
	傳出值 : null
*/
function btnFIND_OnClick()
{
	try
	{
		//　取得使用者選取之rowIndex
		var index = getSelectedIndex();
		if (index < 0) 
		{
			ShowError("尚未選取要查詢的資料, 請確認");
			return false;
		}

		// programId.lang : 子網頁檔案名稱
		//var program_id = programId.lang;
		var action_type = "?action_type=FIND&" +getSingleQueryString(index);
		
		var ArrOfArgu = new Object();
		ArrOfArgu.action_type = "FIND";
	    // 開啟dialog視窗
		var url = getURL(program_id.action_type);
		var settings = getDialogStr(dialogStr);
		if (url != "" && settings != "") {
		    var result = window.showModalDialog(url, ArrOfArgu, settings, true);
		}
		
		// 判斷回傳值,重新Refresh資料
		if (result)
		{
		    btnSEARCH_OnClick();
		}
	}
	catch(e)
	{
		ShowError(e.Message + "\n開起新視窗發生錯誤");
	}
	finally
	{
		delete ArrOfArgu;
	}
}

function getURL(program_id, action_type) {
    var str = program_id.concat(action_type);
    return str;
}

function getDialogStr(dialogStr) {
    var str = dialogStr.concat("");
    return str;
}
/*
	函數功能: 判斷起迄值
	傳入值 : 無
	傳出值 : 無
*/
function Chk_BETWEEN()
{
	try
	{
		var objControl = new Object();	
		var Result_SHEET_NO = new Object();
		var Result_APPLY_NO = new Object();
		var Result_IN_OUT_DATE = new Object();
		var Result_CODE = new Object();
		Result_SHEET_NO = CompareValue("txtSHEET_NO1","txtSHEET_NO2","S")
		Result_CTM_APPLY_NO = CompareValue("txtCTM_APPLY_NO1","txtCTM_APPLY_NO2","S")
		Result_IN_OUT_DATE = CompareValue("txtIN_OUT_DATE1","txtIN_OUT_DATE2","S")
		Result_CODE = CompareValue("txtCODE_1","txtCODE_2","S")
		if(Result_SHEET_NO.id != "0" && Result_SHEET_NO.id != "1")
		{
			objControl.id = "txtSHEET_NO1";
			addDetailErrorItem(objControl,Result_SHEET_NO.val(),$("#lblSHEET_NO").text())
			chk = false;
		}
		else
		{
			objControl.id = "txtSHEET_NO1";
			ErrHandler_RemoveItem(objControl)
		}
		if(Result_CTM_APPLY_NO.id != "0" && Result_CTM_APPLY_NO.id != "1")
		{
			objControl.id = "txtCTM_APPLY_NO1";
			addDetailErrorItem(objControl,Result_CTM_APPLY_NO.val(),$("#lblCTM_APPLY_NO1").text())
			chk = false;
		}
		else
		{
			objControl.id = "txtCTM_APPLY_NO1";
			ErrHandler_RemoveItem(objControl)
		}
		if(Result_IN_OUT_DATE.id != "0" && Result_IN_OUT_DATE.id != "1")
		{
			objControl.id = "txtIN_OUT_DATE1";
			addDetailErrorItem(objControl,Result_IN_OUT_DATE.val(),$("#lblIN_OUT_DATE1").text())
			chk = false;
		}
		else
		{
			objControl.id = "txtIN_OUT_DATE1";
			ErrHandler_RemoveItem(objControl)
		}
		if(Result_CODE.id != "0" && Result_CODE.id != "1")
		{
			objControl.id = "txtCODE_1";
			addDetailErrorItem(objControl,Result_CODE.val(),$("#lblCODE_1").text())
			chk = false;
		}
		else
		{
			objControl.id = "txtCODE_1";
			ErrHandler_RemoveItem(objControl)
		}
	}
	catch(e)
	{
		ShowError(e.Message);
		return false;
	}
	
}